{% extends 'TopxiaWebBundle::bootstrap-modal-layout.html.twig' %}

{% set billable = true %}
{% if (
  (setting('coin.coin_enabled') == 1 and setting('coin.price_type') == 'Coin' and course.coinPrice == 0)  
  or (( not setting('coin.price_type') or setting('coin.price_type') == "RMB" ) and course.price == 0 )
  ) %}
  {% set billable = false %}
{% endif %}
{% block title %}{% if billable %}购买课程{% else %}加入学习{% endif %}{% endblock %}
{% block body %}

  {% if billable and course.type=="live" %}
    <div class="alert alert-info">注意：请在30分钟内完成支付，否则订单将会过期失效</div>
  {% endif %}
  <form id="course-buy-form" class="form-horizontal" method="post" 
  action="{{ path('course_buy_modify_user_info') }}"
  >
    <div class="form-group">
      <div class="col-sm-3 control-label">课程名称</div>
      <div class="col-sm-9 controls">
      	<span class="control-text text-muted">《{{ course.title }}》</span>
        <input type="hidden" name="lessonId" value="{{ lessonId}}">
      </div>
    </div>

    {% if billable %}
    <div class="form-group">
      <div class="col-sm-3 control-label">价格</div>
      <div class="col-sm-9 controls money-text">
      	<span class="control-text">
          {% if vipStatus == 'ok' %}
            会员免费
          {% else %}
            {% if setting('coin.coin_enabled') and setting('coin.price_type') == 'Coin' %}
            <strong class="money">{{ course.coinPrice }}</strong>
            <span class="text-muted">{{setting('coin.coin_name')}}</span>
            {% else %}
            <strong class="money">{{ course.price }}</strong>
        		<span class="text-muted">元</span>
            {% endif %}
          {% endif %}
      	</span>
      </div>
    </div>
    {% endif %}

    {% if billable %}
      {% if not payments %}
        <div class="alert alert-info">{{ setting('payment.disabled_message')|default('尚未开启支付模块，无法购买课程。')|raw }}</div>
      {% endif %}
    {% else %}
      {% if avatarAlert %}
        <div class="alert alert-warning">您还没有头像，设置以后才能加入学习<br />拥有一个独有的头像，老师和同学们能更容易关注到你哦～～ <a href="{{ path('settings_avatar') }}" class="alert-link" target="_blank">点击设置</a></div>
      {% else %}
        <div class="alert alert-info">此课程为免费课程，无需购买，可直接加入学习。</div>
      {% endif %}
    {% endif %}
    <input type="hidden" name="targetId" value="{{ course.id }}" />
    <input type="hidden" name="payment" value="alipay" />
    <input type="hidden" name="_csrf_token" value="{{ csrf_token('site') }}">

    {% set userinfo_enable = setting('course.buy_fill_userinfo')|default(false) %}
    {% set checkedFields = setting('course.userinfoFields')|default([]) %}
    {% include 'TopxiaWebBundle:User:fill-userinfo-fields-block.html.twig' with {isBuyFillUserinfo:userinfo_enable, userinfoFieldsSetting:checkedFields, userFields:userFields, user:user, showNavTip:1} %}

  </form>
{% endblock %}

{% block footer %}
  {% set priceType = setting("coin.price_type") %}
  {% if billable and ((priceType|default("RMB") == "RMB" and course.price > 0) or (priceType|default("RMB") == "Coin" and course.coinPrice > 0)) %}
    {% if payments %}
      {% if member %}
        <span class="text-muted">预览模式无法支付</span>
        <button class="btn btn-primary" disabled="disabled">去支付</button> 
      {% else %}
        {% if vipStatus == 'ok'%}
           <button id="join-course-btn" class="btn btn-primary" data-loading-text="正在加入..." type="submit" data-toggle="form-submit" data-target="#course-buy-form">加入学习</button>
        {% else %}
        <button id="course-pay" class="btn btn-primary" type="submit" data-loading-text="正在创建订单..." data-toggle="form-submit" data-target="#course-buy-form">去支付</button>
        {% endif %}
      {% endif %}
    {% elseif not payments and vipStatus == 'ok' %}
      <button id="join-course-btn" class="btn btn-primary" 
        {% if course.status != 'published' or (course.type == 'live' and course.studentNum >= course.maxStudentNum) %}
        disabled="disabled"
        {% endif %}
        data-loading-text="正在加入..." data-submiting-text="正在加入..." type="submit" data-toggle="form-submit" data-target="#course-buy-form">加入学习</button>
    {% endif %}
  {% else %}
    {% if member %}
      <span class="text-muted">预览模式无法加入学习</span>
      <button class="btn btn-primary" disabled="disabled">加入学习</button> 
    {% else %}  
      {% if not avatarAlert %}
        <button id="join-course-btn" class="btn btn-primary" 
        {% if course.status != 'published' or (course.type == 'live' and course.studentNum >= course.maxStudentNum) %}
        disabled="disabled"
        {% endif %}
        data-loading-text="正在加入..." data-submiting-text="正在加入..." type="submit" data-toggle="form-submit" data-target="#course-buy-form">加入学习</button>
      {% endif %}
    {% endif %}

  {% endif %}

  <script>
    app.load('course/buy-modal');
  </script>
{% endblock %}